package com.wpml.juc;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * @Description
 * @Date 2021-05-18
 * @Author wangpei
 **/
public class AtomicTest {

    static Long count = 0L;
    static AtomicLong atomicLong = new AtomicLong(0);
    static LongAdder longAdder = new LongAdder();
    static final Object lock = new Object();
    static int threadNum = 1_000;
    static int sumNum = 10_0000;



    public static void main(String[] args) throws InterruptedException {

        Semaphore semaphore = new Semaphore(2);

        new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("t1 running ...");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("t1 end ...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                semaphore.release();
            }
        }).start();

        new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("t2 running ...");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("t2 end ...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                semaphore.release();
            }
        }).start();



//        casTest();

    }

    /**
     * threadNum = 1_000    sumNum = 10_0000
     *
     * synchronized 耗时: 3501 result : 100000000
     * atomicLong 耗时: 1802 result : 100000000
     * longAdder 耗时: 211 result : 100000000
     */
    private static void casTest() throws InterruptedException {
        Thread[] threads = new Thread[threadNum];

        // synchronized 测试
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int k = 0; k < sumNum; k++) {
                    synchronized (lock) {
                        count++;
                    }
                }
            });
        }
        long start = System.currentTimeMillis();
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("synchronized 耗时: " + (System.currentTimeMillis() - start) + " result : " + count);

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int k = 0; k < sumNum; k++) {
                    atomicLong.incrementAndGet();
                }
            });
        }
        start = System.currentTimeMillis();
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("atomicLong 耗时: " + (System.currentTimeMillis() - start) + " result : " + atomicLong.get());

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int k = 0; k < sumNum; k++) {
                    longAdder.increment();
                }
            });
        }
        start = System.currentTimeMillis();
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("longAdder 耗时: " + (System.currentTimeMillis() - start) + " result : " + longAdder.longValue());
    }
}
